import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_shop/pages/base/base_presenter.dart';
import 'package:flutter_shop/pages/base/base_state.dart';
import 'package:flutter_shop/res/styles.dart';
import 'package:flutter_shop/routers/navigator_utils.dart';
import 'package:flutter_shop/widget/common_app_bar.dart';
import 'package:flutter_shop/widget/page_state_widget.dart';
import 'package:flutter_shop/widget/tree_select/tree_node.dart';
import 'package:flutter_shop/widget/tree_select/tree_select.dart';

/// Title:TreeState <br/>
/// Description: 需要实现树结构的StatefulWidget的State,继承此TreeState<br/>
/// author:xiemiao <br/>
/// date: 2020-04-21  <br/>
/// version 1.0 <br/>
abstract class TreeState<T extends StatefulWidget, E extends TreeNode> extends BaseState<T, BasePresenter> {
  List<TreeNode> mDatas = List();

  TreeSelectController treeSelectController = TreeSelectController();

  @override
  Widget build(BuildContext context) {
    TreeConfig treeConfig = getTreeConfig();

    Widget child = Gaps.empty;
    if (mDatas != null) {
      child = TreeSelect(
        mDatas,
        controller: treeSelectController,
        expandLevel: treeConfig.expandLevel,
        isLinkSelect: treeConfig.isLinkSelect,
        checkClick: (treeNode) {
          if (!treeConfig.isMultiSelect) {
            setResultReturn([treeNode]);
          }
        },
      );
    }

    return Scaffold(
      appBar: CommonAppBar(
        getTitle() ?? "请选择",
        rightText: treeConfig.isMultiSelect ? "确定" : "",
        rightClick: () {
          List<TreeNode> selectedList = treeSelectController.getSelectedList();
          setResultReturn(selectedList);
        },
      ),
      body: PageStateWidget(
        child: child,
        controller: pageStateController,
        retryClick: () {
          pageStateController.showLoading();
          requestGetTreeList();
        },
      ),
    );
  }

  @override
  void buildComplete() {
    super.buildComplete();
    requestGetTreeList();
  }

  ///获取treelist
  void requestGetTreeList() {
    getTreeList().then((value) {
      if (value == null) {
        value = List();
      }
      showPageData(value);
    }).catchError((err) {
      showError(err.message);
    });
  }

  ///展示页面数据
  void showPageData(List<TreeNode> result) {
    if (result.isEmpty) {
      pageStateController.showEmpty();
    } else {
      pageStateController.showSuccess();
    }
    mDatas.clear();
    mDatas.addAll(result);
    setState(() {});
  }

  ///设置返回结果
  void setResultReturn(List<TreeNode> result) {
    List<E> list = result.map((e) => e as E).toList();
    NavigatorUtils.goBackWithParams(context, list);
  }

  ///页面标题
  String getTitle();

  ///获取树配置
  TreeConfig getTreeConfig();

  ///获取数据
  Future<List<TreeNode>> getTreeList();
}

///树配置
class TreeConfig {
  ///是否多选
  final bool isMultiSelect;

  ///是否关联选择
  final bool isLinkSelect;

  ///设置默认展开层级
  final int expandLevel;

  TreeConfig({
    this.isMultiSelect = true,
    this.isLinkSelect = false,
    this.expandLevel = 0,
  });
}
